package com.smxy.daydayup.util;

import java.util.Objects;

/**
 * 租户上下文
 *
 * @author jinyue
 */
@SuppressWarnings("unused")
public class TenantContext {

    private static final ThreadLocal<Tenant> cacheTenant = new ThreadLocal<>();
    private static final ThreadLocal<RequestSourceDetail> cacheRequestSourceDetail = new ThreadLocal<>();

    public static void clear() {
        cacheTenant.remove();
        cacheRequestSourceDetail.remove();
    }

    public static Tenant getTenant() {
        final Tenant tenant = cacheTenant.get();
        if (tenant == null) {
            return new Tenant("-", "SYS");
        }
        return tenant;
    }

    public static RequestSourceDetail getRequestSourceDetail() {
        return cacheRequestSourceDetail.get();
    }

    public static void init(Tenant tenant) {
        if (Objects.isNull(tenant) ||
                Objects.isNull(tenant.getId()) ||
                (Objects.isNull(tenant.getName()) || "".equals(tenant.getName()))) {
            throw new RuntimeException("tenant init error by without id or name.");
        }
        cacheTenant.set(tenant);
    }

    public static void init(RequestSourceDetail requestSourceDetail) {
        if (Objects.isNull(requestSourceDetail.detail) ||
                Objects.isNull(requestSourceDetail.source)
        ) {
            throw new RuntimeException("requestSourceDetail init error by without detail or source.");
        }
        cacheRequestSourceDetail.set(requestSourceDetail);
    }

    public static void init(Tenant tenant, RequestSourceDetail requestSourceDetail) {
        init(tenant);
        init(requestSourceDetail);
    }

    public enum RequestSource {
        WEB,
        APP,
        WXAPP,
        WXH5,
    }

    public static class RequestSourceDetail {
        public final RequestSource source;
        public final String detail;

        public RequestSourceDetail(RequestSource source, String userAgent) {
            this.source = source;
            this.detail = userAgent;
        }

        public RequestSourceDetail(String userAgent) {
            this.detail = userAgent == null ? "" : userAgent;

            final String agentLowerCase = userAgent.toLowerCase();

            if (agentLowerCase.indexOf("micromessenger") >= 0) {
                this.source = RequestSource.WXH5;
                return;
            }

            if (agentLowerCase.indexOf("miniprogram") >= 0 || agentLowerCase.indexOf("wechatdevtools") >= 0) {
                this.source = RequestSource.WXAPP;
                return;
            }

            if (agentLowerCase.indexOf("kcapp") >= 0) {
                this.source = RequestSource.APP;
                return;
            }


            this.source = RequestSource.WEB;

        }
    }


    public static class Tenant {

        public final String id;

        public final String name;

        public final String[] roles;

        public String getId() {
            return id;
        }

        public String getName() {
            return name;
        }

        public String[] getRoles() {
            return roles;
        }

        public Tenant(String id, String name) {
            this.id = id;
            this.name = name;
            this.roles = null;
        }

    }

}
